<?php
namespace jy_wxshifu;
defined('BY_JYA') or exit('error');
/*
  服务订单
	
*/
class order extends \plugin{
	/*
		系统指派，shifu_id是MID
	*/
	public function order_pai($order){
		$order_id=intval($order['id']);
		$allusers=array();
		if($order['storeid'] && !$order['merchid']){
			$areas=pdo_getall('jy_wxshifu_area',array('uniacid'=>$_SESSION['uniacid'],'store_id OR'=>'store_id=0 or store_id = \''.$order['storeid'].'\''));
			foreach($areas as &$item){
				$users=pdo_getall('jy_wxshifu_member',array('group_id'=>$item['id'],'status'=>1),'mid');
				foreach($users as $uv){
					$allusers[]=$uv;
				}
			}
		}else{
			$users=pdo_getall('jy_wxshifu_member',array('status'=>1,'uniacid'=>$_SESSION['uniacid']),'mid');
			foreach($users as $uv){
					$allusers[]=$uv;
			}
		}
		$nums=count($allusers);
		$key=rand(0,$nums-1);
		$key=$key?:0;
		$shifu_id=$allusers[$key]['mid'];
		if($shifu_id<2){
			return false;
		}
		$params=array('order_id'=>$order_id,'shifu_id'=>$shifu_id);
		$this->order_sendget($params);
		return true;
	}
	/*
		抢单shifu_id是MID
	*/
	public function order_get($params){
		$order_id=intval($params['order_id']);
		$shifu_id=intval($params['shifu_id']);
		$order=pdo_get('jy_wxshifu_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		if($order['status']==2){
			exi('该订单已被别人抢单了！','error');
		}
		$this->create_sendlog($order_id,$shifu_id,1);
		$this->model('notice')->notice_orderget($order['id']);
		$this->model('model')->uporder($order,2,$shifu_id);
		return true;
	}
	/*
		发送订单给师傅
	*/
	public function order_sendget($params){
		$order_id=intval($params['order_id']);
		$shifu_id=intval($params['shifu_id']);
		$order=pdo_get('jy_wxshifu_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$this->create_sendlog($order_id,$shifu_id);
		$this->model('model')->uporder($order,1,$shifu_id);
		$this->model('notice')->notice_orderget($order['id']);
		return true;
	}
	/*
		师傅确定接单
	*/
	public function order_checkget($order_id){
		$order=pdo_get('jy_wxshifu_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$this->up_sendlog($order['id'],$_SESSION['uid'],2);
		$this->model('notice')->notice_orderget($order['id']);
		$this->model('model')->uporder($order,2);
		return true;
	}
	/*
		转单超时
	*/
	public function order_checktime($order_id=0){
		$setting=$this->plugin_setting('setting');
		$send_status13_time=intval($setting['send_status13_time']);
		if(!$send_status13_time){
			return false;
		}
		if($order_id){
			$sendlog=pdo_get('jy_wxshifu_sendlog',array('status'=>0,'mid'=>$_SESSION['uid'],'order_id'=>$order_id));
			if($sendlog['createtime']){
				$lasttime=$sendlog['createtime']+$send_status13_time*60;
				if($lasttime<TIMESTAMP){
					pdo_update('jy_weishop_order',array('shifu_id'=>0,'status'=>1,'send_status'=>12),array('id'=>$order_id));
					$this->up_sendlog($order_id,$sendlog['mid'],3);
				}
			}
		}else{
			$sendlogs=pdo_getall('jy_wxshifu_sendlog',array('status'=>0,'uniacid'=>$_SESSION['uniacid']));
			foreach($sendlogs as $sendlog){
				$order_id=$sendlog['order_id'];
				if($sendlog['createtime']){
					$setting=$this->plugin_setting('setting');
					$lasttime=$sendlog['createtime']+$send_status13_time*60;
					if($lasttime<TIMESTAMP){
						pdo_update('jy_weishop_order',array('shifu_id'=>0,'status'=>1,'send_status'=>12),array('id'=>$order_id));
						$this->up_sendlog($order_id,$sendlog['mid'],3);
					}
				}
			}
		}
		return true;
	}
	/*
		提交完成
	*/
	public function order_upprice($post=array()){
		$order_id=$post['id'];
		$order=pdo_get('jy_wxshifu_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$order['images']=$post['images'];
		$order['upfile']=$post['upfile'];
		$order['remark']=$post['remark'];
		$orderinfo=$this->model('model')->getorder($order,1);
		if($post['price'] && !$orderinfo['ispay']){
			$price=$post['price'];
			$this->model('order')->up_sendlog($order_id,'',4);
			$this->model('model')->uporder($order,6,'',$post['remark'],$price);
		}else{
			$this->model('order')->up_sendlog($order_id,'',2);
			$this->model('model')->uporder($order,3);
		}
		return true;
	}
	/*
		完成订单
	*/
	public function order_receive($order_id){
		$this->create_orderlog($order_id);
		$order=pdo_get('jy_wxshifu_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$this->model('order')->up_sendlog($order_id,'',2);
		$this->model('model')->uporder($order,3);
		return true;
	}
	/*
		接单日志
	*/
	public function create_sendlog($order_id,$shifuid=0,$status=0,$plugin=''){
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'shifuid'=>$shifuid,
				'order_id'=>$order_id,
				'fee'=>$fee,
				'plugin'=>$plugin,
				'status'=>$status,
				'createtime'=>TIMESTAMP,
		);
		$this->insert('sendlog',$data);
		return true;
	}
	/*
		uplog
	*/
	public function up_sendlog($order_id,$shifuid=0,$status=0){
		$this->pdo_update('sendlog',array('status'=>$status),array('shifuid'=>$shifuid,'order_id'=>$order_id,'status <>'=>3,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function add_baobei($order_id,$msg=''){
		$order=pdo_get('jy_wxshifu_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$mid=$mid?:$_SESSION['uid'];
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'order_id'=>$order_id,
			'oldstatus'=>$order['status'],
			'api_pluginname'=>$order['pluginname'],
			'mid'=>$order['mid'],
			'msg'=>$msg?:'异常报备',
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		$this->pdo_insert('baobei_log',$data);
		$logid=pdo_insertid();
		$this->pdo_update('order',array('status'=>5),array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$this->model('model')->uporder($order,5);
		return $logid;
	}
	/*
		创建结算订单
	*/
	function create_orderlog($order_id,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$order=pdo_get('jy_wxshifu_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$orderlog=$this->pdo_get('orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'ordersn'=>$order['ordersn']),array('id'));
		if($orderlog['id']){
			exi('请勿重复提交！','error');
		}
		//按商品/按件
		if($this->setting['shifu_commission_type']){
			$get_price=$get_money=$this->setting['shifu_fee'];
		}else{
			$get_price=$get_money=$this->setting['shifu_fee'];
		}
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'ordersn'=>$order['ordersn'],
				'order_price'=>$order['price'],
				'get_price'=>$get_price,
				'get_money'=>$get_money,
				'createtime'=>TIMESTAMP,
				'status'=>0,
		);
		$this->insert('orderlog',$data);
		return true;
	}
	/*
		订单升级为待结算
	*/
	public function cash_orderlog(){
		$cashdays=$this->setting['cash_day']?:1;
		$cashtime = mktime(0,0,0,date('m'),date('d')-$cashdays,date('Y'));
		$orderlog=pdo_getall('jy_wxshifu_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>0,'createtime <'=>$cashtime));
		foreach($orderlog as $item){
			pdo_update('jy_wxshifu_orderlog',array('status'=>1),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		}
		return true;
	}
	/*
		待结算订单升级为结算完成/未结算
	*/
	public function change_orderlog($mid,$status=0){
		$orderlog=pdo_getall('jy_wxshifu_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1));
		foreach($orderlog as $item){
			pdo_update('jy_wxshifu_orderlog',array('status'=>1),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>$status));
		}
		return true;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'remark'=>$remark,
		);
		$this->update('cashlog',$update,array('id'=>$balance_id));
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type='',$wxgid='',$pay_trade_no=''){
		$balance=$this->get('cashlog',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			//exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$pay_type=$this->pay_types2[$pay_type];
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['getprice'],$balance['id'],$wxgid);
		$update=array(
			'status'=>3,
			'applysn'=>$trade_no=='pay_trade_no'?$pay_trade_no:$trade_no,
		);
		if($pay_type){
			$update['paytype']=$pay_type;
		}
		$this->update('cashlog',$update,array('id'=>$balance['id']));
		return true;
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,$this->plugin_name,$remark='分销佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn,$wxgid=''){
		if($wxgid){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
		}
		$params=array(
			'appid'=>$wxapp['appid']?:'',
			'mch_id'=>$wxapp['mchid']?:'',
			'openid'=>$mid,
			'mch_billno'=>'cassn'.$balancesn,
			'money'=>$money,
			'wxgid'=>$wxgid,
			'remark'=>'代理商佣金发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		return $res['payment_no'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}
}